package com.stevetracey.backup.io.google;

import java.util.HashSet;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.gmail.GmailScopes;
import com.stevetracey.common.Configuration;

public class Google {

	/** Info required for API Authentication */
	protected static final String APPLICATION_NAME = Configuration
			.getValue("application.name");
	private static final String CLIENT_ID = Configuration
			.getValue("google.api.client-id");
	private static final String SECRET = Configuration
			.getValue("google.api.secret");


	/** Data store location for API credentials */
	private static final java.io.File DATA_STORE_DIR = new java.io.File(
			System.getProperty("user.home"), ".store/gdrive_data");

	/** Global instance of the HTTP transport. */
	protected static HttpTransport httpTransport;

	/** Global instance of the JSON factory. */
	protected static final JsonFactory JSON_FACTORY = JacksonFactory
			.getDefaultInstance();
	
	/** Data Store Factory */
	private static FileDataStoreFactory dataStoreFactory;

	private static Log log = LogFactory.getLog(Google.class);
	
	protected static Credential CREDENTIAL;
	
	public static void initialise()
	{
		log.info("Intialising Google Services");
		
		try {
			httpTransport = GoogleNetHttpTransport.newTrustedTransport();
			dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
			
			

			// Authorisation with API
			GoogleClientSecrets clientSecrets = new GoogleClientSecrets();
			GoogleClientSecrets.Details details = new GoogleClientSecrets.Details();
			details.setClientId(CLIENT_ID);
			details.setClientSecret(SECRET);
			clientSecrets.setInstalled(details);

			// Set up Authorization code flow
			Set<String> scopes = new HashSet<String>();
			scopes.add(DriveScopes.DRIVE_METADATA_READONLY);
			scopes.add(DriveScopes.DRIVE_FILE);
			scopes.add(GmailScopes.MAIL_GOOGLE_COM);

			GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
					httpTransport, JSON_FACTORY, clientSecrets, scopes)
					.setDataStoreFactory(dataStoreFactory).build();

			CREDENTIAL = new AuthorizationCodeInstalledApp(flow,
					new LocalServerReceiver()).authorize("user");

			log.info("Logging in with client id [" + CLIENT_ID + "]");

			
			log.info("Initialising Google Drive");
			GoogleDrive.initialiseGoogleDrive();
			log.info("Initialising Google Mail");
			GoogleMail.initialise();
			
		    
		} catch (Exception e) {
			e.printStackTrace();
			log.error("Failure initialising the Google Drive for backup", e);
		}
	}

}
